﻿using Microsoft.AspNetCore.Mvc;
using ST.Buz.Base.Entity;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;

namespace ST.Buz.Base.ViewModel
{
	/// <summary>
	/// 导出文件信息
	/// </summary>
	public class VM_ExportFile
	{
		/// <summary>
		/// 文件名称
		/// </summary>
		public string Name { get; set; }

		/// <summary>
		/// 文件内容
		/// </summary>
		public byte[] Content { get; set; }

		/// <summary>
		/// 文件类型
		/// </summary>
		public string Mime { get; set; }

		public VM_ExportFile()
		{
		}

		public VM_ExportFile(string name, byte[] content)
		{
			Name = name;
			Content = content;
			if (name.EndsWith(".docx"))
			{
				Mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
			}
			else if (name.EndsWith(".doc") || name.EndsWith(".dot"))
			{
				Mime = "application/msword";
			}
			else if (name.EndsWith(".pptx"))
			{
				Mime = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
			}
			//else if (name.EndsWith(".ppt"))
			//{
			//	Mime = "application/msword";
			//}
			else if (name.EndsWith(".xlsx"))
			{
				Mime = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
			}
			else if (name.EndsWith(".xls"))
			{
				Mime = "application/vnd.ms-excel";
			}
			else
			{
				Mime = "application/octet-stream";
			}
		}

		public VM_ExportFile(string name, byte[] content, string mime)
		{
			Name = name;
			Content = content;
			Mime = mime;
		}

		public FileContentResult Export(Controller controller)
		{
			return controller.File(Content, Mime, Name);
		}
	}
}
